package com.joker.orange.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joker.common.core.domain.PageQuery;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.helper.LoginHelper;
import com.joker.orange.domain.MemberOrder;
import com.joker.orange.domain.RedemptionCode;
import com.joker.orange.domain.UserInfo;
import com.joker.orange.domain.vo.MemberOrderVo;
import com.joker.orange.mapper.MemberOrderMapper;
import com.joker.orange.mapper.RedemptionCodeMapper;
import com.joker.orange.mapper.UserInfoMapper;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * AppPersonalCenterService
 */
@RequiredArgsConstructor
@Service
@Transactional
public class AppPersonalCenterService {

    private final RedemptionCodeMapper redemptionCodeMapper;
    private final UserInfoMapper userInfoMapper;
    private final MemberOrderMapper orderMapper;

    /**
     * 会员购买记录
     */
    public TableDataInfo<MemberOrderVo> queryOrderList(Integer pageNum, Integer pageSize){
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(pageNum);
        pageQuery.setPageSize(pageSize);
        LambdaQueryWrapper<MemberOrder> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(MemberOrder::getOrderType, "1");
        wrapper.eq(MemberOrder::getUserId, LoginHelper.getUserId());
        Page<MemberOrderVo> pageList = orderMapper.selectVoPage(pageQuery.build(),wrapper);
        return TableDataInfo.build(pageList);
    }

    /**
     * 查询兑换记录
     */
    public List<RedemptionCode> getRedemptionCodeList() {
        return redemptionCodeMapper.selectList(new LambdaQueryWrapper<RedemptionCode>()
            .eq(RedemptionCode::getUserId, LoginHelper.getUserId())
            .eq(RedemptionCode::getUseStatus,"0"));
    }

    /**
     * 兑换
     */
    public void updateRedemptionCode(String codeName) {
        RedemptionCode redemptionCode = redemptionCodeMapper.selectOne(new LambdaQueryWrapper<RedemptionCode>()
            .eq(RedemptionCode::getUserId, LoginHelper.getUserId())
            .eq(RedemptionCode::getUseStatus,"1"));
        if(redemptionCode == null) {
            throw new RuntimeException("不存在可使用的兑换码");
        }
        if (!codeName.equals(redemptionCode.getCodeName())){
            throw new RuntimeException("兑换码输入错误");
        }
        redemptionCode.setUseStatus("0");
        redemptionCode.setRedemptionTime(new Date());
        redemptionCodeMapper.updateById(redemptionCode);
        UserInfo userInfo = userInfoMapper.selectById(LoginHelper.getUserId());
        if ("0".equals(redemptionCode.getMemberType())){
            if ("0".equals(userInfo.getIsMember())){
                String memberExpireTime = userInfo.getMemberExpireTime();
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                // 获取当前日期
                LocalDate startDate = LocalDate.parse(memberExpireTime, dateTimeFormatter);
                // 计算天会员到期时间
                LocalDate dailyExpiration = startDate.plusDays(Long.parseLong(redemptionCode.getGiveTime()));
               String time = dailyExpiration.format(dateTimeFormatter);
               userInfo.setMemberExpireTime(time);
               userInfoMapper.updateById(userInfo);
            } else {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                // 获取当前日期
                LocalDate now = LocalDate.now();
                // 计算天会员到期时间
                LocalDate dailyExpiration = now.plusDays(Long.parseLong(redemptionCode.getGiveTime()));
                String time = dailyExpiration.format(dateTimeFormatter);
                userInfo.setMemberExpireTime(time);
                userInfoMapper.updateById(userInfo);
            }
        } else {
            if ("0".equals(userInfo.getIsGridMember())){
                String memberExpireTime = userInfo.getGridMemberExpireTime();
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                // 获取当前日期
                LocalDate startDate = LocalDate.parse(memberExpireTime, dateTimeFormatter);
                // 计算天会员到期时间
                LocalDate dailyExpiration = startDate.plusDays(Long.parseLong(redemptionCode.getGiveTime()));
                String time = dailyExpiration.format(dateTimeFormatter);
                userInfo.setGridMemberExpireTime(time);
                userInfoMapper.updateById(userInfo);
            } else {
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                // 获取当前日期
                LocalDate now = LocalDate.now();
                // 计算天会员到期时间
                LocalDate dailyExpiration = now.plusDays(Long.parseLong(redemptionCode.getGiveTime()));
                String time = dailyExpiration.format(dateTimeFormatter);
                userInfo.setGridMemberExpireTime(time);
                userInfoMapper.updateById(userInfo);
            }
        }
    }

}
